/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.edu.itschapala.ssrp.util;

import javax.faces.application.NavigationHandler;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpSession;
import mx.edu.itschapala.ssrp.modelo.Usuario;

/**
 *
 * @author pello
 */
public class ListenerLogueado implements PhaseListener {

    @Override
    public void afterPhase(PhaseEvent event) {

        FacesContext facesContext = event.getFacesContext();
        String currentPage = facesContext.getViewRoot().getViewId();

        boolean isLoginPage = (currentPage.lastIndexOf("indexLogin.xhtml") > -1);

        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);

        if (session == null) {
            NavigationHandler nh = facesContext.getApplication().getNavigationHandler();
            nh.handleNavigation(facesContext, null, "indexLogin.xhtml");
        } else {
            /*Object currentUser = session.getAttribute("username");
             // ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
             //Usuario currentUser= (Usuario)externalContext.getSessionMap().get("USUARIO_LOGUEADO");
             if (!isLoginPage && (currentUser == null)) {
             NavigationHandler nh = facesContext.getApplication().getNavigationHandler();
             nh.handleNavigation(facesContext, null, "loginPage");
             }*/

            Object currentUser = session.getAttribute("USUARIO_LOGUEADO");

            if (!isLoginPage && (currentUser == null || currentUser == "")) {
                NavigationHandler nh = facesContext.getApplication().getNavigationHandler();
                nh.handleNavigation(facesContext, null, "indexLogin.xhtml");
            } /*else {
                if(isLoginPage && (currentUser != null || currentUser != "")){
                ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
                Usuario usuario = (Usuario) externalContext.getSessionMap().get("USUARIO_LOGUEADO");
                NavigationHandler nh = facesContext.getApplication().getNavigationHandler();
                switch (usuario.getPerfil()) {
                    case "vinculacion":
                        nh.handleNavigation(facesContext, null, "indexVinculacion.xhtml");
                        break;
                    case "division":
                        nh.handleNavigation(facesContext, null, "indexDivision.xhtml");
                        break;
                    case "docente":
                        nh.handleNavigation(facesContext, null, "indexAsesor.xhtml");
                        break;
                    case "alumno":
                        nh.handleNavigation(facesContext, null, "indexAlumno.xhtml");
                        break;
                    case "academia":
                        nh.handleNavigation(facesContext, null, "indexAcademia.xhtml");
                        break;
                    case "escolares":
                        nh.handleNavigation(facesContext, null, "indexAcademia.xhtml");
                        break;
                }
            }
            }*/
        }
    }

    @Override
    public void beforePhase(PhaseEvent event
    ) {

    }

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.RESTORE_VIEW;
    }
}
